package com.su.sweet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.su.sweet.model.SUser;
import com.su.sweet.mapper.SUserMapper;
import com.su.sweet.service.ISUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.snack.core.utils.StringUtil;
import org.noear.solon.annotation.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author su
 * @since 2025-01-02
 */
@Component
public class SUserServiceImpl implements ISUserService {
    @Db
    private SUserMapper userMapper;

    @Override
    public Map<String, Object> selectUserList(SUser user, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Page<SUser> userIPage = userMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<SUser>().like(!StringUtil.isEmpty(user.getUsername()),"username",user.getUsername())
                .like(!StringUtil.isEmpty(user.getPhone()),"phone",user.getPhone()).like(!StringUtil.isEmpty(user.getNickname()),"nickname",user.getNickname()));
        map.put("list", userIPage.getRecords());
        map.put("total", userIPage.getTotal());
        return map;
    }

    @Override
    public SUser login(SUser user) {
        return userMapper.selectOne(new QueryWrapper<SUser>().eq("username", user.getUsername()).eq("password", user.getPassword()));
    }

    @Override
    public Integer insertUser(SUser user) {
        return userMapper.insert(user);
    }

    @Override
    public Integer updateUser(SUser user) {
        return userMapper.updateById(user);
    }

    @Override
    public Integer deleteUser(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public List<SUser> getALlUser() {
        return userMapper.selectList(null);
    }
}
